#include "QbModule.h"
#include "QbbridgeMsgProcess.h"
//#include "FakeWidget.h"
#include "BestQuoteWidget.h"
#include "BondInfoWidget.h"
#include "HomePage.h"
#include <widgets/SBaseDialog.h>
#include <QMessageBox>
#include <QLibrary>
#include <QApplication>
#include <QAbstractNativeEventFilter>
#include <QPixmap>
#include <QBitmap>
#include <core/FileSys.h>

extern "C" S_QB_EXPORT IModule * getModule()
{
	return &QbModule::instance();
}

QbModule& QbModule::instance()
{
	static QbModule s_module;
	return s_module;
}

void QbModule::initialize(IApplication* app)
{
    m_app = app;
	qbbridge::InitDecodeFuncs();
}

void QbModule::unInitialize()
{
}

int QbModule::execCommand(CmdInfo ci) const
{
	IMainFrame* frame = m_app->mainFrame();
	QString paraStr = QString::fromUtf8(ci.parameters);
	QString titleStr = QString::fromUtf8(ci.title);

    //模块可以自己决定是否多开
	int tabId = frame->existCmdWindow(ci.cmdId, paraStr);
	if (tabId != -1)
		frame->activateCmdWindow(tabId);
	else
	{
        QWidget* widget = nullptr;
        auto iter = m_functions.find(ci.actualCmdId);
		if (iter != m_functions.end())
            widget = iter->second(paraStr);

        int actualCmdId = ci.actualCmdId;
        if (20002 == actualCmdId)
        {
            widget = new BestQuoteWidget(titleStr);
        }
		//else if (20044 == actualCmdId)
		//{
		//	widget = new HomePage();
		//}
		//else if (77000 == actualCmdId)
		//{
		//	widget = new QbWebWidget(frame, titleStr, paraStr);
		//	widget = new FakeWidget(titleStr);	
		//}

		//if (widget == nullptr)
		//{
        //    widget = new FakeWidget(titleStr);
		//}
		if(widget)
			frame->openCmdWindow(ci.cmdId, paraStr, widget, titleStr);
	}
	return 0;
}

bool QbModule::closeCommandWindow(int cmdId, QWidget* widget)
{
	if (!widget)
		return true;

	widget->setParent(nullptr);
	delete widget;

	return true;
}
